package com.fs.cv.controller;

import com.fs.cv.pojo.pojodao.User;
import com.fs.cv.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @工程名: SSM项目
 * @描述：
 * @作者: 小梁子
 * @创建时间: 2020-09-28 14:32
 **/

@Controller
@RequestMapping("/page")
public class PageController {


    //自动装配userservice
    @Autowired
    UserService userService;

    //跳过登录界面直接跳转到主页
    @RequestMapping("index")
    public String login() {
        return "index";
    }

    //跳转到欢迎页面
    @RequestMapping("welcom")
    public String welcom() {
        return "welcom";
    }


    //主页跳转到教师成绩管理模板

    /**
     * 方法描述:将学生的成绩显示在成绩管理的前端页面上，业务逻辑在StudentCourseController里面
     *
     * @参数 []
     * @返回值 java.lang.String
     * @作者 苏明友
     * @时间 2020/9/29
     */
    @RequestMapping("/scoreData")
    public String studentScore() {
        return "redirect:/studentCourse/getStudentCourse";
    }

    //主页跳转到学生信息管理模板
    @RequestMapping("/studentInfo")
    public String studentInfo() {
        return "studentInfo";
    }

    //主页跳转到教室管理模板
    @RequestMapping("/classroomInfo")
    public String clssroomInfo() {
        return "classroomInfo";
    }


    //主页跳转到学生考勤管理模板
    @RequestMapping("/absence")
    public String absenc() {
        return "absence";
    }

    //主页跳转到教师信息管理模板
    @RequestMapping("/teacherInfo")
    public String teacherInfo() {
        return "teacherInfo";
    }

    //主页跳转到课程信息管理模板
    @RequestMapping("/courseInfo")
    public String courseInfo() {

        return "course";
    }

    //主页跳转到班级信息管理模板
    @RequestMapping("/classInfo")
    public String classInfo() {
        return "classInfo";
    }

    //主页跳转到授课信息管理模板
    @RequestMapping("/recordInfo")
    public String recordInfo() {
        return "redirect:/education/teaching/beforeGet";
    }

    //主页跳转到院系管理模板
    @RequestMapping("/departmentInfo")
    public String departmentInfo() {
        return "departmentInfo";
    }

    //主页跳转到用户管理模板
    @RequestMapping("/userInfo")
    public String userInfo() {
        return "userInfo";
    }


    /**
     * 方法描述:
     *
     * @param user
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @author 周先华
     * @date 2020/9/28
     */
    @RequestMapping("/login")
    @ResponseBody
    public Map<String, Object> login(User user, String loginCode, String remember, HttpServletRequest req) {
        Map<String, Object> result = new HashMap<>();
        String rightCod = (String) req.getSession().getAttribute("rightCod");
        System.out.println(user);

        if (!loginCode.equals(rightCod)) {//验证码不正确
            result.put("code", 0);
        } else {
            result = userService.login(user.getUserUserName(), user.getUserPassword(), remember);
        }
        return result;
    }


    @RequestMapping("/logout")
    public String logout(String code, HttpServletRequest req) {
        userService.logout();
        return "login";
    }

    @RequestMapping("/log")//自动登录验证
    public String log() {
        userService.logout();
        return "login";
//      Subject subject = SecurityUtils.getSubject();
//      System.out.println("*********************************************");
//      System.out.println(subject.isAuthenticated());
//      System.out.println(subject.isRemembered());
//      System.out.println(subject.getPrincipal().toString());
//      // 如果 isAuthenticated 为 false 证明不是登录过的，
//      // 同时 isRememberd 为true 证明是没登陆直接通过记住我功能进来的
//      if (!subject.isAuthenticated() && subject.isRemembered()) {
//          Object principal = subject.getPrincipal();
//          System.out.println(String.valueOf(principal));
//          if (null != principal) {
//              User user = userService.queryUserInfoByUserName(String.valueOf(principal));
//              String password = user.getUserPassword();
//              UsernamePasswordToken token = new UsernamePasswordToken(user.getUserUserName(), password);
//              token.setRememberMe(true);
//              subject.login(token);//登录
//              return"index";
//          }
//      }
//      return "login";
    }

    @RequestMapping("/unauthorized")
    public String unauthorized() {
        return "unauthorized";
    }

}
